#!/bin/bash


PROJECT_ROOT="$HOME/.config/scripts/fzf-sql"
# set -a
# PROJECT_ROOT=$(dirname `readlink --canonicalize "${BASH_SOURCE[0]}"`)

si=0
set_server() {
    servers[$si]="$1"
    cmds1[$si]="$2"
    cmds2[$si]="$3"
    targets[$si]="$4"
    copytext[$si]="$5"
    si=$(($si+1))
}

# 载入数据配置文件
if [[ -f "$PROJECT_ROOT/databases.sh" ]]; then
    source "$PROJECT_ROOT/databases.sh"
else
    cp "$PROJECT_ROOT/databases-example.sh" "$PROJECT_ROOT/databases.sh"
    # [ -f ./databases.sh ] && source ~/.config/scripts/fzf-sshscp/databases.sh
    source "$PROJECT_ROOT/databases-example.sh"
fi

# 通用公共函数
## fzf输出预览列表函数
_list() {
    for ((i = 0; i < ${#servers[@]}; i++)); do
        _item=${servers[$i]}
        echo "${_item[@]}"
    done
}

_index() {
    for ((i = 0; i < ${#servers[@]}; i++)); do
        _item=${servers[$i]}
        [[ "$_item" == "$1" ]] && echo $i && break
    done
}

_copytext() {
    index=$(_index "$1")
    echo -n ${copytext[index]}
}

# 

_cmds1() {
    index=$(_index "$1")
    # 导入命令拼接
    echo ${cmds1[index]}
}

_cmds2() {
    index=$(_index "$1")
    # 导出命令拼接
    echo ${cmds2[index]} 
}

## 特定私有处理函数

### 预览可连接数据库列表
_preview1() {
    index=$(_index "$1")
    echo  ${targets[index]}
}

_preview2() {
    index=$(_index "$1")
    target="$2"
    [ "$target" = "连接" ] && echo "${servers[index]} : ${cmds1[index]} ${targets[index]}"
    [ "$target" = "导入" ] && echo "${servers[index]} : ${cmds2[index]} ${targets[index]} 数据库名 < 数据库文件.sql"
    [ "$target" = "导出" ] && echo "${servers[index]} : ${cmds1[index]} ${targets[index]} 数据库名 > 导出的文件名.sql "
}

_preview2_import() {
    index=$(_index "$1")
    target="$2"
    [ "$target" = "连接" ] && echo "${servers[index]} : ${cmds1[index]} ${targets[index]}"
    [ "$target" = "导入" ] && echo "${servers[index]} : ${cmds2[index]} ${targets[index]} 数据库名 < 数据库文件.sql"
    [ "$target" = "导出" ] && echo "${servers[index]} : ${cmds1[index]} ${targets[index]} 数据库名 > 导出的文件名.sql "
}

_preview2_export() {
    index=$(_index "$1")
    target="$2"
    [ "$target" = "连接" ] && echo "${servers[index]} : ${cmds1[index]} ${targets[index]}"
    [ "$target" = "导入" ] && echo "${servers[index]} : ${cmds2[index]} ${targets[index]} 数据库名 < 数据库文件.sql"
    [ "$target" = "导出" ] && echo "${servers[index]} : ${cmds1[index]} ${targets[index]} 数据库名 > 导出的文件名.sql "
}

### 主函数：数据库连接
_mysql_c() {

    # 默认fzfopt操作
    fzfopt='--cycle  --header-first  --preview-window=bottom:1:wrap --height=81%'


    # 选择服务器
    # server=$(_list | fzf $fzfopt --header=请选择服务器 --preview="~/.config/scripts/fzf-sshscp/scp.sh preview1 {}")
    # server=$(_list | fzf \
    #     --cycle \
    #     --header='请选择服务器' \
    #     --header-first \
    #     --preview="~/.config/scripts/fzf-sshscp/sql.sh preview1 {}" \
    #     --height=81% \
    #     --preview-window=bottom:1:wrap
    # )
    server=$(_list | fzf $fzfopt \
                    --header='请选择数据库' \
                    --preview="${PROJECT_ROOT}/sql.sh preview1 {}" \
            )

    [[ -z "$server" ]] && exit 0
    # 选择方向
    # target=$(echo -e "连接\n导出\n导入" | fzf \
    #     --cycle \
    #     --header='请选择操作' \
    #     --header-first \
    #     --preview="${PROJECT_ROOT}/sql.sh preview2 $server {}" \
    #     --height=81% \
    #     --preview-window=bottom:1:wrap
    # )
    target=$(echo -e "连接\n导出\n导入" | fzf $fzfopt \
        --header='请选择操作' \
        --preview="${PROJECT_ROOT}/sql.sh preview2 $server {}" \
    )
    [[ -z "$target" ]] && exit 0

    # 复制密码到剪贴板 
    # echo -e "copy $(_copytext "$server") ..."
    echo -e "copy "$(_preview1 $server)" passwd  ..."
    echo -e "$(_copytext "$server")" | pbcopy  
    echo -e "copy success."


    # 连接数据库
    # 执行链接命令
    if [[ "$target" = "连接" ]]; then
        echo -e "数据库连接命令"
        _cmd="$(_cmds1 $server) $(_preview1 $server)"
    fi

    # 
    if [[ "$target" = "导出" ]]; then
        echo -e "导出数据库：('$(_preview1 $server)')"
        # echo -e 'ssh '$(_preview1 $server)' ls'
        # echo -e $(_sshcmdstr "$server") $(_preview1 $server)' ls'
        echo -e '导出命令开发中'

        # 组装命令执行进入远程数据库主机查看数据库列表

        # 选择导出文件存放目录

        # 组装导出命令
        # _cmd="scp -r $(_preview1 $server):$_source ."
        # _cmd="$(_scpcmdstr "$server") $(_preview1 $server):$_source ."
        _cmd="导出命令开发中"
    fi
    
    if [[ "$target" = "导入" ]]; then
        echo -e "导入数据库：('$(_preview1 $server)')"
        # echo -e 'ssh '$(_preview1 $server)' ls'
        # echo -e $(_sshcmdstr "$server") $(_preview1 $server)' ls'
        echo -e '导入命令开发中'

        # 组装命令执行进入远程数据库主机查看数据库列表

        # 选择导入数据库文件

        # 组装最后命令

        # _cmd="scp -r $(_preview1 $server):$_source ."
        # _cmd="$(_scpcmdstr "$server") $(_preview1 $server):$_source ."
        _cmd="导入命令开发中"
    fi

    # [[ -z "$_source" ]] && exit 0

    # 选择操作
    # option=$(echo -e "执行命令\n复制命令" | fzf $fzfopt --header=请选择操作 --preview="echo $_cmd" --height=7)
    option=$(echo -e "执行命令\n复制命令" | fzf  \
                                            --cycle \
                                            --header-first \
                                            --preview-window=bottom:1:wrap \
                                            --header='请选择操作' \
                                            --preview="echo $_cmd" \
                                            --height=81% \
    )

    [[ -z "$option" ]] && exit 0

    # 执行
    # linux
    # [ "$option" = "复制命令" ] && echo -n $_cmd | xsel -b copy && echo 'copied'
    # [ "$option" = "执行命令" ] && echo 开始执行: $_cmd && eval $_cmd

    # macOS
    [ "$option" = "复制命令" ] && echo -n $_cmd | pbcopy  && echo 'copied'
    [ "$option" = "执行命令" ] && echo 开始执行: $_cmd && eval $_cmd
    
}

### 数据库导出
### 数据库导入

# 主入口
case $1 in
    '') _mysql_c ;;
    preview1) _preview1 "$2" ;;
    preview2) _preview2 "$2" "$3" ;;
    preview2_connect) _preview2_connect "$2" "$3" ;;
    preview2_import) _preview2_import "$2" "$3" ;;
    preview2_export) _preview2_export "$2" "$3" ;;
    copytext) _copytext "$2" ;;
    # cmdstr) _cmdstr "$2" ;;
    *) echo "其他" ;;
esac

